package ashok.corejava.threads;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchApp {

	public static void main(String []args){
		CountDownLatch latch = new CountDownLatch(3);
		ExecutorService exec =  Executors.newFixedThreadPool(3);
		
		for(int i=0;i<3;i++){
			exec.submit(new CProcessor(latch));
		}
		
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Completed...");
	}
}


class CProcessor implements Runnable{

	private CountDownLatch latch;
	
	public CProcessor(CountDownLatch latch){
		this.latch = latch;
	}
	@Override
	public void run() {
		System.out.println("Started...");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		latch.countDown();
	}
	
}